package dingding

import (
	"bytes"
	"encoding/json"
	"net/http"
)

// TextMessage 文本信息
type TextMessage struct {
	MsgType string       `json:"msgtype"`
	Text    *TextContent `json:"text"`
	At      *AtInfo      `json:"at"`
}

// TextContent 文本内容
type TextContent struct {
	Content string `json:"content"`
}

// AtInfo at信息
type AtInfo struct {
	AtMobiles []string `json:"atMobiles"`
	IsAtAll   bool     `json:"isAtAll"`
}

func postMessage(webHookAddr string, phones []string, message string) error {
	msg := &TextMessage{
		MsgType: "text",
		Text: &TextContent{
			Content: message,
		},
		At: &AtInfo{
			AtMobiles: phones,
			IsAtAll:   false,
		},
	}

	buf, err := json.Marshal(msg)
	if err != nil {
		return err
	}

	buffer := bytes.NewBuffer(buf)
	_, err = http.Post(webHookAddr, "application/json", buffer)
	if err != nil {
		return err
	}

	return nil
}
